// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameEvent from "../../Commons/GameEvent";
import MapManager, { MapEvent } from "../../Models/Maps/MapManager";
import BaseUI from "../BaseUI";
import MapItem from "../UIItems/MapItem";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MapUI extends BaseUI {
    @property(cc.Prefab)
    mapItemPrefab: cc.Prefab = null;
    @property(cc.ScrollView)
    scrollview: cc.ScrollView = null;
    @property([MapItem])
    itemArr: MapItem[] = [];


    protected onLoad(): void {
        // var arr = MapManager.single.getMapTemplates();

        // for (let i = 0; i < this.itemArr.length; i++) {
        //     var obj = cc.instantiate(this.mapItemPrefab);
        //     obj.active = false;
        //     this.scrollview.content.addChild(obj);
        // }
        for (const iterator of this.itemArr) {
            iterator.node.active = false;
        }
    }

    protected onEnable(): void {
        this.drawView();

        GameEvent.eventNode.on(MapEvent.SELECTMAP_EVENT, this.onSelectMap, this);
    }

    protected onDisable(): void {
        GameEvent.eventNode.off(MapEvent.SELECTMAP_EVENT, this.onSelectMap, this);
    }
    onSelectMap() {
        this.drawView();
    }

    drawView() {

        var arr = MapManager.single.getMapTemplates();
        for (let i = 0; i < this.itemArr.length; i++) {
            this.itemArr[i].node.active = true;
            this.itemArr[i].drawItem(arr[i]);
        }
    }
}
